//
//  SNCompilePlanCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/26.
//  Copyright © 2016年 stone. All rights reserved.
//
#import "SNSummaryInfoViewController.h"
#import "UIImageView+TapGesture.h"
#import "SNCompilePlanSummaryCell.h"
#import "SNPlanListLabel.h"
#import "SNSeedViewController.h"
#import "SNPlanListViewController.h"
#import "Masonry.h"
@interface SNCompilePlanSummaryCell()
@property (weak, nonatomic) IBOutlet UILabel *seedNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *startTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *plantingAreaLabel;
/** 地块信息Label */
@property (weak, nonatomic) IBOutlet UILabel * blockInformationLabel;

@property (weak, nonatomic) IBOutlet UIView *unfoldView;

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageViewHeightLayoutConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *unfoldViewHeightLayoutConstraint;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *leftLabels;
@property (weak, nonatomic) IBOutlet UIButton *unfoldButton;


@end
@implementation SNCompilePlanSummaryCell

-(void)setProductionPlanListResult:(SNProductionPlanListResult *)productionPlanListResult{
    _productionPlanListResult = productionPlanListResult;
    self.seedNameLabel.text = [NSString stringWithFormat:@"* 作物种类 : %@",self.productionPlanListResult.cropTypeName];
    self.startTimeLabel.text = [NSString stringWithFormat:@"* 种植时间 : %@",self.productionPlanListResult.startTime];
    self.plantingAreaLabel.text = [NSString stringWithFormat:@"* 种植面积 : %@公顷",self.productionPlanListResult.plantingArea];
    
    for (UIView *label in _unfoldView.subviews) {
        if ([label isKindOfClass:[UIButton class]]) {
            continue;
        }
        [label removeFromSuperview];
    }
    
    CGSize size = [[[NSAttributedString alloc] initWithString:@"北坡柏树底-旱地-100公顷" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]}] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    
    for (int i = 0;i < self.productionPlanListResult.landList.count;i++) {
        SNProductionPlanListLandList *land = self.productionPlanListResult.landList[i];
        
        
            UILabel * label = [[UILabel alloc] init];
//            /** GPU 优化 */
//            label.opaque = YES;
//            label.backgroundColor = [UIColor whiteColor];
//            label.layer.masksToBounds = YES;
            //------------------------------
            label.numberOfLines = 1;
            label.textColor = HexRGB(0xAFAFAF);
            label.textAlignment = NSTextAlignmentLeft;
//            NSLog(@"%@",land.landLocation);
        
            NSString * location = [land.landLocation substringWithRange:NSMakeRange(0, 2)];
            label.text = [NSString stringWithFormat:@"%@-%@-%@公顷",location,land.landTypeName,land.plantArea];
            label.font = [UIFont systemFontOfSize:13];
            
            // [self addSubview:label];
            // [self.view addSubview:label];
            [self.unfoldView addSubview:label];
            
            [label mas_makeConstraints:^(MASConstraintMaker * make) {
                make.left.mas_equalTo(self.blockInformationLabel.mas_right).offset(0);
                make.right.mas_equalTo(self.unfoldButton.mas_left).offset(0);
                make.top.mas_equalTo(self.blockInformationLabel.mas_top).offset(3+i*(size.height+13));
            }];
        
        
//        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30*(i+1))];
//        label.text = [NSString stringWithFormat:@"%@-%@-%@",land.landLocation,land.landType,land.availableArea];
//        [_unfoldView addSubview:titleLabel];
    }
    
    CGFloat height = self.productionPlanListResult.landList.count * 26.5;
    if (height == 0) {
        height = 21;
    }
    self.unfoldViewHeightLayoutConstraint.constant = self.unfoldButton.selected?height:21;
    self.unfoldView.layer.masksToBounds = YES;
    
    [self.leftLabels enumerateObjectsUsingBlock:^(UILabel *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [SNUtils specialLabelWithstring:obj.text keyString:@"*" label:obj color:HexRGB(0xFF5D04)];
    }];
}

- (void)setModel:(SNPlanListModel *)model{
    _model = model;

    self.seedNameLabel.text = [NSString stringWithFormat:@"* 作物种类 : %@",self.model.seedName];
    self.startTimeLabel.text = [NSString stringWithFormat:@"* 种植时间 : %@",self.model.startTime];
    self.plantingAreaLabel.text = [NSString stringWithFormat:@"* 种植面积 : %@公顷",self.model.plantingArea];

    for (UIView *label in _unfoldView.subviews) {
        if ([label isKindOfClass:[UIButton class]]) {
            continue;
        }
        [label removeFromSuperview];
    }

    for (int i = 0;i<self.model.landList.count;i++) {
        Land *land = self.model.landList[i];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30*(i+1))];
        label.text = [NSString stringWithFormat:@"%@-%@-%@",land.landLocation,land.LandType,land.availableArea];
        [_unfoldView addSubview:label];
    }
    [self awakeFromNib];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        CALayer * layer = [CALayer layer];
        
        layer.frame = CGRectMake(50, 0, [UIScreen mainScreen].bounds.size.width - 50 - 15 - 12, 1);
        /** color */
        layer.backgroundColor = HexRGB(0xE2E2E2).CGColor;
        
        [self.contentView.layer addSublayer:layer];
    }
    return self;
}
- (void)configCellWithTitle:(NSString *)title imageName:(NSString *)imageName{
    self.iconImageView.image = [UIImage imageNamed:imageName];
    //    self.leftLabel.text = [NSString stringWithFormat:@"* %@",title];
    //
    //    [SNUtils specialLabelWithstring:self.leftLabel.text keyString:@"*" label:self.leftLabel color:HexRGB(0xFF5D04)];
    
    
    UIImage * image = self.iconImageView.image;
    
    self.iconImageViewHeightLayoutConstraint.constant = 20 * image.size.height / image.size.width;

}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.leftLabels enumerateObjectsUsingBlock:^(UILabel *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [SNUtils specialLabelWithstring:obj.text keyString:@"*" label:obj color:HexRGB(0xFF5D04)];
    }];
    self.unfoldButton.selected = YES;
    self.unfoldButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
    
    UIImage * image = self.iconImageView.image;
    
    self.iconImageViewHeightLayoutConstraint.constant = 20 * image.size.height / image.size.width;
}
- (IBAction)unfoldButtonClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        sender.imageView.transform = CGAffineTransformMakeRotation(M_PI);
    }else{
        sender.imageView.transform = CGAffineTransformIdentity;
    }
    
    CGFloat height = self.productionPlanListResult.landList.count * 26.5;
    if (height == 0) {
        height = 21;
    }
    self.unfoldViewHeightLayoutConstraint.constant = sender.selected?height:21;
    self.unfoldView.layer.masksToBounds = YES;
    
    [((UITableView *)self.superview.superview) reloadData];
}

@end
